	var hostserver="";  
	var zTree;  
	var treeNodes;
	var setting = {  
	   		view: {
	   			  //dblClickExpand: false,
			  expandSpeed: 100 //设置树展开的动画速度
		},
		check: { 
			autoCheckTrigger:false,
			chkboxType:{ "Y": "ps", "N": "ps" },
			chkStyle:"checkbox",
			nocheckInherit:false,
			chkDisabledInherit:false,
		    enable: true, 
		    radioType:"all"
		},  
		//获取json数据
	    data: {
			simpleData: {
				enable: true,
				idKey: "id",
				pIdKey: "pId",
				rootPId: 0
			}
		},  
	    callback : {  
	        onClick : function(event, treeId, treeNode, clickFlag) {  
	            if(true) {
	        	 //控制栏目层级两层 
	            	$("#parentId").val(treeNode.id);
	            	$("#parentName").val(treeNode.name);
	            }  
	        },  
	        onAsyncSuccess : function(event, treeId, treeNode, msg){  
	        }  
	    }  
	};
	 function loadData(server){ 
		 var roleid=$("#roleid").val();
		 hostserver=server;
		 var url=server+"/admin/auth/role/loadMenuData";
		 var param="type=jsonTree";
		 if(roleid!=null&&roleid!=""){
			 param+="&roleid="+roleid;
		 }
		  $.ajax({
			    url: url, 
			    data: param,
				async : false,  
		        cache:false,  
		        type: 'get',  
		        dataType : "json",  
		        error: function (){//请求失败处理函数  
		            alert('请求失败');  
		        },  
		        success:function(data){  
			       treeNodes = data;   //把后台封装好的简单Json格式赋给treeNode 
		       }  
		    });  
	     $.fn.zTree.init($("#tree"), setting,treeNodes);
	 }
	  
  function saveRole(){
	  var roleid=$("#roleid").val();
      var rolename=$("#rolename").val();
      var oprtype=$("#oprtype").val();
      var description=$("#description").val();
      if(rolename==""||rolename.length<1){
    	  if($("#rolename").val().length >=2 && $("#rolename").val().length <20){
     		 $("#rolename").next().text(' 输入正确').removeClass('red').addClass('green');
     	}else if($("#rolename").val().length<2){
			$("#rolename").next().html('&nbsp;* 角色名称过短').removeClass('green').addClass('red');
			return false; 
		}else if($('#rolename').val().length>30){
			$("#rolename").next().html('&nbsp;* 角色名称过长').removeClass('green').addClass('red');
			return false; 
		}
      }
      
      var param="oprtype="+oprtype+"&roleid="+roleid+"&rolename="+rolename;
      //拼接描述内容
      if(description!=null){
    	  param+="&description="+description;
      }
      //拼接管理菜单那权限内容
	  var treeObj = $.fn.zTree.getZTreeObj("tree");
      var nodes = treeObj.getCheckedNodes();
      var menueids="";
      for (var i = 0; i < nodes.length; i++) {
    	  menueids+=nodes[i].id+",";
      }
      param+="&menueids="+menueids;
      var url=hostserver+"/admin/auth/role/save";
      jQuery.ajax({
			type: "post",
			url: url,
			data: param,
			cache: false,
			contenttype: "text/html;charset=utf-8",
			success: function(result){
				if(result=='1'){
					window.location.href=hostserver+"/admin/auth/role/list";
				}
			},
			error:function(){
				//alert("请求延时..");
			}
		});  
  }
  $(function(){
      // 验证用户名
      $('#rolename').blur(function(){
          if($(this).val().length >= 2 && $(this).val().length <=12 && $(this).val()!=''){
          	$(this).next().html('&nbsp;* <span class="icon-ok"></span>').removeClass('red').addClass('green'); 
              name=true;
          }else if($(this).val().length<2){
				$(this).next().html('&nbsp;* 角色名称过短').removeClass('green').addClass('red');
				return false; 
			}else if($(this).val().length>30){
				$(this).next().html('&nbsp;* 角色名称过长').removeClass('green').addClass('red');
				return false; 
			}
      });
});